﻿using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Security;

namespace JobBack.Models
{
    public class UTIdentity : IIdentity
    {

        /// <summary>
        /// forms 身份验证票据
        /// </summary>
        private FormsAuthenticationTicket ticket;
        /// <summary>
        /// http请求连接
        /// </summary>
        private HttpContext context = HttpContext.Current;
        /// <summary>
        /// 用户基本信息
        /// </summary>
        private UserData _userData;
        #region Property
        /// <summary>
        /// 认证方式
        /// </summary>
        public string AuthenticationType
        {
            get { return "Forms"; }
        }
        /// <summary>
        /// 名称(登录名）
        /// </summary>
        public string Name
        {
            get { return _userData.AdminName; }
        }
        /// <summary>
        /// 通过验证
        /// </summary>
        public bool IsAuthenticated
        {
            get { return true; }
        }
        /// <summary>
        /// 用户Cookie数据
        /// </summary>
        public UserData UserData
        {
            get { return _userData; }
        }
        /// <summary>
        /// 用户ID
        /// </summary>
        public int Id
        {
            get { return _userData.AdminId; }
        }
        private string mIP = "";
        /// <summary>
        /// 用户IP
        /// </summary>
        public string IP
        {
            get
            {
                if (string.IsNullOrEmpty(context.Request.UserHostAddress))
                {
                    try
                    {
                        mIP = context.Request.UserHostAddress;
                    }
                    catch { }
                }
                return mIP;
            }
        }
        /// <summary>
        /// 是否是管理员
        /// </summary>
        public bool IsAdmin
        {
            get { return 1 > 0; }
        }
        /// <summary>
        /// 用户姓名
        /// </summary>
        public string UserName { get; private set; }
        /// <summary>
        /// 部门
        /// </summary>
        public string RoleName { get; private set; }
        #endregion

        public UTIdentity(FormsAuthenticationTicket ticket)
        {
            _userData = (UserData)Newtonsoft.Json.JsonConvert.DeserializeObject(ticket.UserData, typeof(UserData));
            this.ticket = ticket;
        }
    }
}